<!DOCTYPE html>
<html lang="zh_cn">
<head>
          <title>来玩魔王的咚</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta charset="utf-8" />
        <!-- twitter card metadata -->
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="/images/mowang.png">
<meta name="twitter:site" content="">
<meta name="twitter:title" content="shell 创建文本菜单">
<meta name="twitter:description" content="<p>创建文本菜单：echo和case定制菜单；select工具快速创建文本菜单；dialog包制作窗口</p>">
        <!-- OG Tags -->
<meta property="og:url" content="/shell-text-menu.html"/>
<meta property="og:title" content="来玩魔王的咚 | shell 创建文本菜单" />
<meta property="og:description" content="<p>创建文本菜单：echo和case定制菜单；select工具快速创建文本菜单；dialog包制作窗口</p>" />
        <!-- favicon -->
        <link rel="icon" type="image/png" href="/images/mowang.png">
        <!-- moment.js for date formatting -->
        <script src="/theme/js/moment.js"></script>
        <!-- css -->
        <link rel="stylesheet" type="text/css" href="/theme/css/main.css" />
        <!-- 左边的menu，如果页面高度不够，就跟着滚动，否则文章分类显示不全 -->
        <link rel="stylesheet" type="text/css" href="/theme/css/mycss/menu.css" />
		<script>
			
                /*! grunt-grunticon Stylesheet Loader - v2.1.2 | https://github.com/filamentgroup/grunticon | (c) 2015 Scott Jehl, Filament Group, Inc. | MIT license. */
    
    (function(e){function t(t,n,r,o){"use strict";function a(){for(var e,n=0;u.length>n;n++)u[n].href&&u[n].href.indexOf(t)>-1&&(e=!0);e?i.media=r||"all":setTimeout(a)}var i=e.document.createElement("link"),l=n||e.document.getElementsByTagName("script")[0],u=e.document.styleSheets;return i.rel="stylesheet",i.href=t,i.media="only x",i.onload=o||null,l.parentNode.insertBefore(i,l),a(),i}var n=function(r,o){"use strict";if(r&&3===r.length){var a=e.navigator,i=e.Image,l=!(!document.createElementNS||!document.createElementNS("http://www.w3.org/2000/svg","svg").createSVGRect||!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")||e.opera&&-1===a.userAgent.indexOf("Chrome")||-1!==a.userAgent.indexOf("Series40")),u=new i;u.onerror=function(){n.method="png",n.href=r[2],t(r[2])},u.onload=function(){var e=1===u.width&&1===u.height,a=r[e&&l?0:e?1:2];n.method=e&&l?"svg":e?"datapng":"png",n.href=a,t(a,null,null,o)},u.src="",document.documentElement.className+=" grunticon"}};n.loadCSS=t,e.grunticon=n})(this);(function(e,t){"use strict";var n=t.document,r="grunticon:",o=function(e){if(n.attachEvent?"complete"===n.readyState:"loading"!==n.readyState)e();else{var t=!1;n.addEventListener("readystatechange",function(){t||(t=!0,e())},!1)}},a=function(e){return t.document.querySelector('link[href$="'+e+'"]')},c=function(e){var t,n,o,a,c,i,u={};if(t=e.sheet,!t)return u;n=t.cssRules?t.cssRules:t.rules;for(var l=0;n.length>l;l++)o=n[l].cssText,a=r+n[l].selectorText,c=o.split(");")[0].match(/US\-ASCII\,([^"']+)/),c&&c[1]&&(i=decodeURIComponent(c[1]),u[a]=i);return u},i=function(e){var t,o,a;o="data-grunticon-embed";for(var c in e)if(a=c.slice(r.length),t=n.querySelectorAll(a+"["+o+"]"),t.length)for(var i=0;t.length>i;i++)t[i].innerHTML=e[c],t[i].style.backgroundImage="none",t[i].removeAttribute(o);return t},u=function(t){"svg"===e.method&&o(function(){i(c(a(e.href))),"function"==typeof t&&t()})};e.embedIcons=i,e.getCSS=a,e.getIcons=c,e.ready=o,e.svgLoadedCallback=u,e.embedSVG=u})(grunticon,this);
                
                grunticon(["/theme/css/icons.data.svg.css", "/theme/css/icons.data.png.css", "/theme/css/icons.fallback.css"]);
            </script>
        <noscript><link href="/theme/css/icons.fallback.css" rel="stylesheet"></noscript>
        <!-- menu toggle javascript -->
        <script type="text/javascript">
            document.addEventListener("DOMContentLoaded", initMenu);
            
            function initMenu(){
                var menu = document.getElementById("menu");
                var menulink = document.getElementById("menu-link");
                menulink.addEventListener("click", function toggleMenu(){
                        window.event.preventDefault();
                        menulink.classList.toggle('active');
                        menu.classList.toggle('active');              
                    });
            };
        </script>
        <!-- 不蒜子 -->
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <meta name="description" content="<p>创建文本菜单：echo和case定制菜单；select工具快速创建文本菜单；dialog包制作窗口</p>" />

    <meta name="tags" content="shell" />
  <!-- 替换部分base的样式，看文章时，要再宽一点，右边有很多空间可以撑开 -->
  <link rel="stylesheet" type="text/css" href="/theme/css/mycss/article.css" />

</head>
<body>
    <div role="banner" id="masthead">
        <header>
            <a href="/"><img src="/images/mowang.png" alt="McManus Logo"></a>
                <h1>来玩魔王的咚@骑士救兵</h1>
            <a href="#menu" id="menu-link">more stuff</a>
            <nav id="menu">
                <ul>
                        <li><a href="/tags">tags</a></li>
                            <li><a href="/category/cloud.html">Cloud</a></li>
                            <li><a href="/category/docker.html">Docker</a></li>
                            <li><a href="/category/go.html">Go</a></li>
                            <li class="active"><a href="/category/linux.html">Linux</a></li>
                            <li><a href="/category/python.html">Python</a></li>
                            <li><a href="/category/xue-xi-bi-ji.html">学习笔记</a></li>
                            <li><a href="/category/yun-wei-zi-dong-hua.html">运维自动化</a></li>
                </ul>
            </nav>
        </header>
    </div>
        <div class="page" role="main">
  <div class="article" role="article">
    <article>
        <footer>
            <a name="top"></a>
            <p>
              <time datetime=" 2020-09-22 14:15:00+08:00">
                <script>document.write(moment('2020-09-22 14:15:00+08:00').format('LL'));</script>
              </time>
              ~
              <time datetime=" 2020-09-22 14:15:00+08:00">
                <script>document.write(moment('2020-09-22 14:45:00+08:00').format('LL'));</script>
              </time>
            </p>
        </footer>
        <header>
          <h2>
            shell 创建文本菜单
          </h2>
        </header>
      <div class="content">
         <div class="toc">
<ul>
<li><a href="#chuang-jian-wen-ben-cai-dan">创建文本菜单</a><ul>
<li><a href="#chuang-jian-cai-dan-bu-ju">创建菜单布局</a></li>
<li><a href="#chuang-jian-zhuang-han-shu">创建桩函数</a></li>
<li><a href="#tian-jia-cai-dan-luo-ji">添加菜单逻辑</a></li>
</ul>
</li>
<li><a href="#shi-yong-select-ming-ling">使用 select 命令</a><ul>
<li><a href="#shi-li-dai-ma">示例代码</a></li>
</ul>
</li>
<li><a href="#zhi-zuo-chuang-kou-dialog-bao">制作窗口（dialog包）</a><ul>
<li><a href="#chuang-kou-bu-jian">窗口部件</a></li>
<li><a href="#huo-qu-bu-jian-de-shu-chu">获取部件的输出</a></li>
<li><a href="#shi-li-dai-ma_1">示例代码</a></li>
<li><a href="#an-zhuang-dialog-bao">安装dialog包</a></li>
<li><a href="#dialog-bu-jian-xiang-biao">dialog部件详表</a></li>
<li><a href="#dialog-xuan-xiang">dialog选项</a></li>
</ul>
</li>
</ul>
</div>
<h3 id="chuang-jian-wen-ben-cai-dan"><a class="toclink" href="#chuang-jian-wen-ben-cai-dan">创建文本菜单</a></h3>
<p>核心是case命令，根据用户的选择来执行特定的命令。  </p>
<h4 id="chuang-jian-cai-dan-bu-ju"><a class="toclink" href="#chuang-jian-cai-dan-bu-ju">创建菜单布局</a></h4>
<p>使用echo命令打印字符，生成一个菜单，还可以包括标题等信息：</p>
<div class="highlight"><pre><span></span><code>clear
<span class="nb">echo</span>
<span class="c1"># -e 选项，打印特殊字符</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\t\t\tSys Admin Menu\n&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\t1. Display disk space&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\t2. Display logged on users&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\t3. Display memory usage&quot;</span>
<span class="nb">echo</span> -e <span class="s2">&quot;\t0. Exit program\n\n&quot;</span>
<span class="c1"># -en 会去掉末尾的换行符，光标会留在行尾</span>
<span class="nb">echo</span> -en <span class="s2">&quot;\t\tEnter option: &quot;</span>
<span class="c1"># 获取用户输入，只期望获取到单个字符，-n选择限制只读1个字符，并且用户不用回车</span>
<span class="nb">read</span> -n <span class="m">1</span> option
</code></pre></div>

<p>clear命令，先清空当前会话的内容。<br>
echo -e选项，可以打印特殊字符。<br>
echo -en选项，会去掉末尾的换行符。这让菜单看上去更专业，光标会一直行尾等待用户输入。  </p>
<p><strong>获取用户输入</strong><br>
打印出菜单后，就要等待并获取用户输入。这步使用read命令。这里期望只要单个字符，所以用 -n 选项限制只读取一个字符。这样用户只需要输入一个数字，并且不用按回车。  </p>
<p><strong>创建菜单函数</strong><br>
把上面的部分封装成一个函数，这样，在任何时候只要调用函数，就能重现菜单。  </p>
<h4 id="chuang-jian-zhuang-han-shu"><a class="toclink" href="#chuang-jian-zhuang-han-shu">创建桩函数</a></h4>
<p><strong>桩函数</strong>（stub function），是一个空函数，或者只有一个echo语句，说明最终这里需要什么内容：</p>
<div class="highlight"><pre><span></span><code><span class="k">function</span> diskspace <span class="o">{</span>
    clear
    <span class="nb">echo</span> <span class="s2">&quot;Display disk space&quot;</span>
<span class="o">}</span>
</code></pre></div>

<p>这样，就不需要事先写出所有函数。菜单能够直接投入使用，之后在来实现具体的操作。<br>
<em>再不如写python的时候，先只写一个函数签名，函数体就一行pass。之后再来补充具体的代码。</em>  </p>
<h4 id="tian-jia-cai-dan-luo-ji"><a class="toclink" href="#tian-jia-cai-dan-luo-ji">添加菜单逻辑</a></h4>
<p>菜单布局和函数都有了，下面需要创建程序逻辑将二者结合起来。这里就需要case命令。<br>
case命令根据菜单中输入的字符来调用相应的函数。用默认的case命令字符星号来处理所有不正确的菜单项。<br>
下面就是这个完整的菜单脚本的例子：</p>
<div class="highlight"><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="c1"># 为脚本创建文本菜单</span>

<span class="c1"># --------------------</span>
<span class="c1"># 定义函数</span>
<span class="c1"># --------------------</span>

<span class="c1"># 打印菜单</span>
<span class="k">function</span> menu <span class="o">{</span>
    clear
    <span class="nb">echo</span>
    <span class="c1"># -e 选项，打印特殊字符</span>
    <span class="nb">echo</span> -e <span class="s2">&quot;\t\t\tSys Admin Menu\n&quot;</span>
    <span class="nb">echo</span> -e <span class="s2">&quot;\t1. Display disk space&quot;</span>
    <span class="nb">echo</span> -e <span class="s2">&quot;\t2. Display logged on users&quot;</span>
    <span class="nb">echo</span> -e <span class="s2">&quot;\t3. Display memory usage&quot;</span>
    <span class="nb">echo</span> -e <span class="s2">&quot;\t0. Exit program\n\n&quot;</span>
    <span class="c1"># -en 会去掉末尾的换行符，光标会留在行尾</span>
    <span class="nb">echo</span> -en <span class="s2">&quot;\t\tEnter option: &quot;</span>
    <span class="c1"># 获取用户输入，只期望获取到单个字符，-n选择限制只读1个字符，并且用户不用回车</span>
    <span class="nb">read</span> -n <span class="m">1</span> option
<span class="o">}</span>

<span class="k">function</span> diskspace <span class="o">{</span>
    clear
    df -k
<span class="o">}</span>

<span class="k">function</span> whoseon <span class="o">{</span>
    clear
    who
<span class="o">}</span>

<span class="k">function</span> menusage <span class="o">{</span>
    clear
    cat /proc/meminfo
<span class="o">}</span>

<span class="c1"># --------------------</span>
<span class="c1"># 函数主体</span>
<span class="c1"># --------------------</span>
<span class="k">while</span> <span class="o">[</span> <span class="m">1</span> <span class="o">]</span>
<span class="k">do</span>
    <span class="c1"># 菜单逻辑</span>
    menu
    <span class="k">case</span> <span class="nv">$option</span> <span class="k">in</span>
    <span class="m">0</span><span class="o">)</span>
        <span class="nb">break</span> <span class="p">;;</span>
    <span class="m">1</span><span class="o">)</span>
        diskspace <span class="p">;;</span>
    <span class="m">2</span><span class="o">)</span>
        whoseon <span class="p">;;</span>
    <span class="m">3</span><span class="o">)</span>
        menusage <span class="p">;;</span>
    *<span class="o">)</span>
        clear
        <span class="nb">echo</span> <span class="s2">&quot;Wrong selection&quot;</span><span class="p">;;</span>
    <span class="k">esac</span>
    <span class="nb">echo</span> -en <span class="s2">&quot;\n\n\t\tHit any key to continue&quot;</span>
    <span class="nb">read</span> -n <span class="m">1</span> line
<span class="k">done</span>
clear
</code></pre></div>

<p>菜单的显示效果如下：</p>
<div class="highlight"><pre><span></span><code>                        <span class="nv">Sys</span> <span class="nv">Admin</span> <span class="nv">Menu</span>

        <span class="mi">1</span>. <span class="nv">Display</span> <span class="nv">disk</span> <span class="nv">space</span>
        <span class="mi">2</span>. <span class="nv">Display</span> <span class="nv">logged</span> <span class="nv">on</span> <span class="nv">users</span>
        <span class="mi">3</span>. <span class="nv">Display</span> <span class="nv">memory</span> <span class="nv">usage</span>
        <span class="mi">0</span>. <span class="k">Exit</span> <span class="nv">program</span>


                <span class="nv">Enter</span> <span class="nv">option</span>: 
</code></pre></div>

<h3 id="shi-yong-select-ming-ling"><a class="toclink" href="#shi-yong-select-ming-ling">使用 select 命令</a></h3>
<p>创建文本菜单的过程中，花了一半的精力在建立菜单布局和获取用户输入上。bash shell 提供了一个很容易上手的小工具，可以帮助自动完成这些工作。  </p>
<p>select命令只需要一条命令就可以创建出菜单，然后获取输入并自动处理。命令格式如下：</p>
<div class="highlight"><pre><span></span><code><span class="k">select</span> 选项变量 <span class="k">in</span> <span class="s2">&quot;选项1&quot;</span> <span class="s2">&quot;选项2&quot;</span> <span class="s2">&quot;选项3&quot;</span>
<span class="k">do</span>
    命令
<span class="k">done</span>
</code></pre></div>

<p>select命令会将每个选项自动加上编号，然后为选项显示一个由PS3环境变量定义的特殊提示符。所以还要定义PS3环境变量。  </p>
<h4 id="shi-li-dai-ma"><a class="toclink" href="#shi-li-dai-ma">示例代码</a></h4>
<p>这里是一个select命令的示例：</p>
<div class="highlight"><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="c1"># 为脚本创建文本菜单</span>

<span class="k">function</span> diskspace <span class="o">{</span>
    clear
    df -k
<span class="o">}</span>

<span class="k">function</span> whoseon <span class="o">{</span>
    clear
    who
<span class="o">}</span>

<span class="k">function</span> menusage <span class="o">{</span>
    clear
    cat /proc/meminfo
<span class="o">}</span>

<span class="nv">PS3</span><span class="o">=</span><span class="s2">&quot;Enter option: &quot;</span>
<span class="k">select</span> option <span class="k">in</span> <span class="s2">&quot;Display disk space&quot;</span> <span class="s2">&quot;Display logged on users&quot;</span> <span class="se">\</span>
<span class="s2">&quot;Display memory usage&quot;</span> <span class="s2">&quot;Exit program&quot;</span>
<span class="k">do</span>
    <span class="k">case</span> <span class="nv">$option</span> <span class="k">in</span>
    <span class="s2">&quot;Exit program&quot;</span><span class="o">)</span>
        <span class="nb">break</span> <span class="p">;;</span>
    <span class="s2">&quot;Display disk space&quot;</span><span class="o">)</span>
        diskspace <span class="p">;;</span>
    <span class="s2">&quot;Display logged on users&quot;</span><span class="o">)</span>
        whoseon <span class="p">;;</span>
    <span class="s2">&quot;Display memory usage&quot;</span><span class="o">)</span>
        menusage <span class="p">;;</span>
    *<span class="o">)</span>
        clear
        <span class="nb">echo</span> <span class="s2">&quot;Wrong selection&quot;</span><span class="p">;;</span>
    <span class="k">esac</span>
<span class="k">done</span>
</code></pre></div>

<p>菜单效果如下：</p>
<div class="highlight"><pre><span></span><code>$ menu2.sh 
1) Display disk space       3) Display memory usage
2) Display logged on users  4) Exit program
Enter option: 
</code></pre></div>

<p>使用这个工具可以快速的创建一个简易的菜单，不过视觉效果就差很多。  </p>
<h3 id="zhi-zuo-chuang-kou-dialog-bao"><a class="toclink" href="#zhi-zuo-chuang-kou-dialog-bao">制作窗口（dialog包）</a></h3>
<p>dialog包能够用ANSI转义控制字符在文本环境中创建标准的窗口对话框。  </p>
<h4 id="chuang-kou-bu-jian"><a class="toclink" href="#chuang-kou-bu-jian">窗口部件</a></h4>
<p>dialog包提供了很多窗口部件（widget），使用的时候命令格式如下：</p>
<div class="highlight"><pre><span></span><code>dialog --widget parameters
</code></pre></div>

<p>常用的部件如下：</p>
<ol>
<li>msgbox部件：在窗口中显示一条简单的消息，会有一个OK按钮</li>
<li>yesno部件：允许用户对窗口中显示的问题选择yes或no，会有两个按钮</li>
<li>inputbox部件：提供一个简单的文本框区域来输入文本</li>
<li>textbox部件：可以在窗口中显示大量信息，会生成一个滚动窗口</li>
<li>menu部件：创建文本菜单，需要为每个选项提供一个选择标号和显示文本</li>
<li>fselect部件：可以用来浏览文件和文件夹</li>
</ol>
<p><em>更多窗口部件，后面会详细列出。</em>  </p>
<h4 id="huo-qu-bu-jian-de-shu-chu"><a class="toclink" href="#huo-qu-bu-jian-de-shu-chu">获取部件的输出</a></h4>
<p>每个dialog部件都提供了两种形式的输出：</p>
<ul>
<li>使用退出状态码</li>
<li>使用STDERR</li>
</ul>
<p><strong>返回选项</strong><br>
如果选择了YES或OK，返回退出状态码0。如果选择了Cancel或No，返回退出状态码1。可以用标准的$?变量来判断具体选择了哪个按钮。  </p>
<p><strong>返回数据</strong><br>
如果是返回数据，则会将数据发送到STDERR。可以用标准的bash shell方法来将STDERR输出重定向到另一个文件或文件描述符中：</p>
<div class="highlight"><pre><span></span><code>dialog --inputbox <span class="s2">&quot;Enter your age: &quot;</span> <span class="m">10</span> <span class="m">20</span> <span class="m">2</span>&gt;age.txt
</code></pre></div>

<p>具体怎么用可以参考下面的示例。  </p>
<h4 id="shi-li-dai-ma_1"><a class="toclink" href="#shi-li-dai-ma_1">示例代码</a></h4>
<p>内容比较多，感觉也不一定需要制作这么好的窗口来交换。编写脚本时记住两件事：</p>
<ul>
<li>如果有按钮，检查dialog命令的退出状态码</li>
<li>否则就重定向STDERR来获取输出的值</li>
</ul>
<p>前面的例子使用dialog包实现的示例代码：</p>
<div class="highlight"><pre><span></span><code><span class="ch">#!/bin/bash</span>
<span class="c1"># 为脚本创建文本窗口菜单</span>

<span class="nv">temp</span><span class="o">=</span><span class="k">$(</span>mktemp -t test.XXXXXX<span class="k">)</span>
<span class="nv">temp2</span><span class="o">=</span><span class="k">$(</span>mktemp -t test2.XXXXXX<span class="k">)</span>

<span class="k">function</span> diskspace <span class="o">{</span>
    df -k &gt; <span class="nv">$temp</span>
    dialog --textbox <span class="nv">$temp</span> <span class="m">20</span> <span class="m">60</span>
<span class="o">}</span>

<span class="k">function</span> whoseon <span class="o">{</span>
    who &gt; <span class="nv">$temp</span>
    dialog --textbox <span class="nv">$temp</span> <span class="m">20</span> <span class="m">50</span>
<span class="o">}</span>

<span class="k">function</span> menusage <span class="o">{</span>
    cat /proc/meminfo &gt; <span class="nv">$temp</span>
    dialog --textbox <span class="nv">$temp</span> <span class="m">20</span> <span class="m">50</span>
<span class="o">}</span>

<span class="k">while</span> <span class="o">[</span> <span class="m">1</span> <span class="o">]</span>
<span class="k">do</span>
dialog --menu <span class="s2">&quot;Sys Admin Menu&quot;</span> <span class="m">20</span> <span class="m">30</span> <span class="m">10</span> <span class="se">\</span>
<span class="m">1</span> <span class="s2">&quot;Display disk space&quot;</span> <span class="se">\</span>
<span class="m">2</span> <span class="s2">&quot;Display logged on users&quot;</span> <span class="se">\</span>
<span class="m">3</span> <span class="s2">&quot;Display memory usage&quot;</span> <span class="se">\</span>
<span class="m">0</span> <span class="s2">&quot;Exit program&quot;</span> <span class="m">2</span>&gt; <span class="nv">$temp2</span>
<span class="k">if</span> <span class="o">[</span> <span class="nv">$?</span> -eq <span class="m">1</span> <span class="o">]</span>
<span class="k">then</span>
    <span class="nb">break</span>
<span class="k">fi</span>

<span class="nv">selection</span><span class="o">=</span><span class="k">$(</span>cat <span class="nv">$temp2</span><span class="k">)</span>
<span class="k">case</span> <span class="nv">$selection</span> <span class="k">in</span>
<span class="m">0</span><span class="o">)</span>
    <span class="nb">break</span> <span class="p">;;</span>
<span class="m">1</span><span class="o">)</span>
    diskspace <span class="p">;;</span>
<span class="m">2</span><span class="o">)</span>
    whoseon <span class="p">;;</span>
<span class="m">3</span><span class="o">)</span>
    menusage <span class="p">;;</span>
*<span class="o">)</span>
    dialog --msgbox <span class="s2">&quot;Wrong selection&quot;</span> <span class="m">10</span> <span class="m">30</span>
<span class="k">esac</span>
<span class="k">done</span>
rm -f <span class="nv">$temp</span> <span class="m">2</span>&gt; /dev/null
rm -f <span class="nv">$temp2</span> <span class="m">2</span>&gt; /dev/null
</code></pre></div>

<p>这里用到了临时文件，并且使用mktemp命令来创建临时文件，看着挺专业。具体可以看这篇: <a href="https://steeed.gitee.io/shell-script-advanced.html#mktemp">https://steeed.gitee.io/shell-script-advanced.html#mktemp</a>  </p>
<h4 id="an-zhuang-dialog-bao"><a class="toclink" href="#an-zhuang-dialog-bao">安装dialog包</a></h4>
<p>另外系统可能默认没有安装dialog包，要运行这个脚本，需要先安装dialog包：</p>
<div class="highlight"><pre><span></span><code><span class="o">[</span>root@Ansible ~<span class="o">]</span><span class="c1"># yum info dialog</span>
已加载插件：fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
已安装的软件包
名称    ：dialog
架构    ：x86_64
版本    ：1.2
发布    ：5.20130523.el7
大小    ：505 k
源    ：installed
来自源：base
简介    ： A utility <span class="k">for</span> creating TTY dialog boxes
网址    ：http://invisible-island.net/dialog/dialog.html
协议    ： LGPLv2
描述    ： Dialog is a utility that allows you to show dialog boxes <span class="o">(</span>containing
         : questions or messages<span class="o">)</span> <span class="k">in</span> TTY <span class="o">(</span>text mode<span class="o">)</span> interfaces.  Dialog is called
         : from within a shell script.  The following dialog boxes are implemented:
         : yes/no, menu, input, message, text, info, checklist, radiolist, and
         : gauge.
         : 
         : Install dialog <span class="k">if</span> you would like to create TTY dialog boxes.
</code></pre></div>

<h4 id="dialog-bu-jian-xiang-biao"><a class="toclink" href="#dialog-bu-jian-xiang-biao">dialog部件详表</a></h4>
<p><strong>dialog部件</strong></p>
<table>
<thead>
<tr>
<th>部件</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>calendar</td>
<td>提供选择日期的日历</td>
</tr>
<tr>
<td>checklist</td>
<td>显示多个选项（其中每个选项都能打开或关闭）</td>
</tr>
<tr>
<td>form</td>
<td>构建一个带有标签及文本字段（可以填内容）的表单</td>
</tr>
<tr>
<td>fselect</td>
<td>提供一个文件选择窗口来浏览选择文件</td>
</tr>
<tr>
<td>gauge</td>
<td>显示完成的百分比进度条</td>
</tr>
<tr>
<td>infobox</td>
<td>显示一条消息，但不用等待回应</td>
</tr>
<tr>
<td>inputbox</td>
<td>提供一个输入文本用的文本表单</td>
</tr>
<tr>
<td>inputmenu</td>
<td>提供一个可编辑的菜单</td>
</tr>
<tr>
<td>menu</td>
<td>显示可选择的一系列选项</td>
</tr>
<tr>
<td>msgbox</td>
<td>显示一条消息，并要求用户选择OK按钮</td>
</tr>
<tr>
<td>pause</td>
<td>显示一个进度条来显示暂停期间的状态</td>
</tr>
<tr>
<td>passwordbox</td>
<td>显示一个文本框，但会隐藏输入的文本</td>
</tr>
<tr>
<td>passwordform</td>
<td>x牧场一个带标签和隐藏文本字段的表单</td>
</tr>
<tr>
<td>radiolist</td>
<td>提供一组菜单选项，单只能选择其中一个。就是单选</td>
</tr>
<tr>
<td>tailbox</td>
<td>用tail命令在滚动窗口中显示文件的内容</td>
</tr>
<tr>
<td>tailboxbg</td>
<td>更tailbox一样，但是在后台模式中运行</td>
</tr>
<tr>
<td>textbox</td>
<td>在滚动窗口中显示文件的内容</td>
</tr>
<tr>
<td>timebox</td>
<td>提供一个选择小时、分钟和秒的窗口</td>
</tr>
<tr>
<td>yesno</td>
<td>提供一条带有Yes和No按钮的简单消息</td>
</tr>
</tbody>
</table>
<h4 id="dialog-xuan-xiang"><a class="toclink" href="#dialog-xuan-xiang">dialog选项</a></h4>
<p>除了标准部件，还可以在dialog命令中定制不同的选项。这些选项可以让你全面定制窗口外观和操作。<br>
<strong>dialog命令选项</strong></p>
<table>
<thead>
<tr>
<th>选项</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>--add-widget</td>
<td>继续下个对话框，直到按下Esc或Cancel按钮</td>
</tr>
<tr>
<td>--aspect <em>ratio</em></td>
<td>指定窗口宽度和高度的宽高比</td>
</tr>
<tr>
<td>--backtitle <em>title</em></td>
<td>指定显示在屏幕顶部背景上的标题</td>
</tr>
<tr>
<td>--begin <em>x</em> <em>y</em></td>
<td>指定窗口左上角的起始位置</td>
</tr>
<tr>
<td>--clear</td>
<td>用默认的对话背景色来清空屏幕内容</td>
</tr>
<tr>
<td>--colors</td>
<td>在对话文本中嵌入ANSI色彩编码</td>
</tr>
<tr>
<td>--cr-wrap</td>
<td>在对话文本中允许使用换行符并强制换行</td>
</tr>
<tr>
<td>--create-rc <em>file</em></td>
<td>将示例配置文件的内容复制到指定的file文件中</td>
</tr>
<tr>
<td>--default-item <em>string</em></td>
<td>设定复选列表、表单或菜单对话中的默认项</td>
</tr>
<tr>
<td>--help</td>
<td>显示dialog命令的帮助信息</td>
</tr>
<tr>
<td>--help-status</td>
<td>当选定Help按钮后，在帮助信息后写入多选列表、单选列表或表单信息</td>
</tr>
<tr>
<td>--ignore</td>
<td>忽略dialog不能识别的选项</td>
</tr>
<tr>
<td>--input-fd <em>fd</em></td>
<td>指定STDIN之外的另一个文件描述符</td>
</tr>
<tr>
<td>--insecure</td>
<td>在password部件中键入内容时显示星号</td>
</tr>
<tr>
<td>--item-help</td>
<td>为多选列表、单选列表或菜单中的每个标号在屏幕底部添加一个帮助栏</td>
</tr>
<tr>
<td>--keep-window</td>
<td>不要清除屏幕上显示过的部件</td>
</tr>
<tr>
<td>--max-input <em>size</em></td>
<td>指定输入的最大字符串长度。默认为2048</td>
</tr>
<tr>
<td>--no-collapse</td>
<td>不要将对话文本中的制表符转换成空格</td>
</tr>
<tr>
<td>--no-kill</td>
<td>将tailbox对话框放到后台，并禁止该进程的SIGHUP信号</td>
</tr>
<tr>
<td>--no-shadow</td>
<td>不要显示对话窗口的阴影效果</td>
</tr>
<tr>
<td>--output-fd <em>fd</em></td>
<td>指定除STDERR之外的另一个输出文件描述符</td>
</tr>
<tr>
<td>--print-maxsize</td>
<td>将对话窗口的最大尺寸打印到输出中</td>
</tr>
<tr>
<td>--print-size</td>
<td>将对话窗口的大小尺寸打印到输出中</td>
</tr>
<tr>
<td>--print-version</td>
<td>将dialog版本号打印到输出中</td>
</tr>
<tr>
<td>--separate-output</td>
<td>一次一行地输出checklist部件的结果，不使用引号</td>
</tr>
<tr>
<td>--separator <em>string</em></td>
<td>指定用于分隔部件输出的字符串</td>
</tr>
<tr>
<td>--separate-widget <em>string</em></td>
<td>指定用于分隔部件输出的字符串</td>
</tr>
<tr>
<td>--shadow</td>
<td>在每个窗口的右下角绘制阴影</td>
</tr>
<tr>
<td>--single-quoted</td>
<td>需要时对多选列表的输出采用单引号</td>
</tr>
<tr>
<td>--sleep <em>sec</em></td>
<td>在处理完对话窗口之后延迟指定的秒数</td>
</tr>
<tr>
<td>--stderr</td>
<td>将输出发送到STDERR（默认行为）</td>
</tr>
<tr>
<td>--stdout</td>
<td>将输出发送到STDOUT</td>
</tr>
<tr>
<td>--tab-correct</td>
<td>将制表符转换成空格</td>
</tr>
<tr>
<td>--tab-len <em>n</em></td>
<td>指定一个制表符占用的空格数（默认为8）</td>
</tr>
<tr>
<td>--timeout <em>sec</em></td>
<td>指定无用户输入时，sec秒后退出并返回错误代码</td>
</tr>
<tr>
<td>--titel <em>title</em></td>
<td>指定对话窗口的标题</td>
</tr>
<tr>
<td>--trim</td>
<td>从对话文本中删除前导空格和换行符</td>
</tr>
<tr>
<td>--visit-items</td>
<td>修改对话窗口中制表符的停留位置，使其包括选项列表</td>
</tr>
</tbody>
</table>
<p>&nbsp;<br>
<strong>dialog命令选项2</strong><br>
按钮的选项功能都差不多，单独列出在这里。可以重写对话窗口中的任意按钮标签：</p>
<table>
<thead>
<tr>
<th>选项</th>
<th>描述</th>
</tr>
</thead>
<tbody>
<tr>
<td>--cancel-label <em>label</em></td>
<td>指定Cancel按钮的替代标签</td>
</tr>
<tr>
<td>--defaultno</td>
<td>将yes/no对话框的默认答案设为No</td>
</tr>
<tr>
<td>--exit-label <em>label</em></td>
<td>指定Exit按钮的替代标签</td>
</tr>
<tr>
<td>--extra-button</td>
<td>在OK按钮和Cancel按钮之间显示一个额外按钮</td>
</tr>
<tr>
<td>--extra-label <em>label</em></td>
<td>指定额外按钮的替代标签</td>
</tr>
<tr>
<td>--help-button</td>
<td>在OK按钮和Cancel按钮后显示一个Help按钮</td>
</tr>
<tr>
<td>--help-label <em>label</em></td>
<td>指定Help按钮的替代标签</td>
</tr>
<tr>
<td>--nocancel</td>
<td>隐藏Cancel按钮</td>
</tr>
<tr>
<td>--no-lable <em>label</em></td>
<td>为No按钮指定替代标签</td>
</tr>
<tr>
<td>--ok-label <em>label</em></td>
<td>指定OK按钮的替代标签</td>
</tr>
<tr>
<td>--yes-label <em>label</em></td>
<td>为Yes按钮指定替代标签</td>
</tr>
</tbody>
</table>
<p>&nbsp;<br>
<strong>举例说明</strong>  </p>
<ul>
<li>--title选项，允许你设置出现在窗口顶部的部件标题。</li>
<li>--backtitle选项，是为脚本中的菜单创建公共标题的简便办法。</li>
<li>--create-rc选项，dialog命令支持运行时配置。该命令会根据配置文件模板创建一份配置文件。</li>
</ul>
      </div>
      <div class="back-to-top">
        <a href="/">HOME</a>
        <a href="#top">TOP</a>
      </div>
    </article>
  </div>
<!-- end article -->
<!-- 页面往下滚动一段之后才会显示TOC -->
<script>
  window.onscroll = function() {
    var tocbox = document.getElementsByClassName('toc')[0];
    var osTop = document.documentElement.scrollTop || document.body.scrollTop;
    var osWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
    // console.log(osTop)
    if (osTop>300 && osWidth>865) {
      tocbox.style.display = "block"
    }
    if (osTop<300 || osWidth<865) {
      tocbox.style.display = "none"
    }
  }
</script>
                <footer>
                    <div class="icons">
                    </div>
                    <span id="busuanzi_container_page_pv" style="padding: 10px">本文阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                    <span id="busuanzi_container_site_pv" style="padding: 10px">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
                    <span id="busuanzi_container_site_uv" style="padding: 10px">本站总访客数<span id="busuanzi_value_site_uv"></span>人</span>
                    <p>© <script>document.write(moment().format('YYYY'));</script> 749B</p>
                </footer>
        </div>
</body>
</html>